package com.shinowit.web;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.mapper.MerchandiseUnitInfoMapper;
import com.shinowit.model.DataGridResult;
import com.shinowit.model.MerchandiseUnitInfo;
import com.shinowit.model.MerchandiseUnitInfoCriteria;
import com.shinowit.model.Result;

@Controller
@RequestMapping(value="/system/unit_info")
public class MerchandiseUnitInfoController {

	@Resource
	private MerchandiseUnitInfoMapper unit_info_dao;
	

	@RequestMapping(value={"","/"},method=RequestMethod.GET)
	public String defaultPage(){
		return "/merchandise_unit_info/list";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public DataGridResult<MerchandiseUnitInfo> getPage(@RequestParam(value="page",defaultValue="1") Integer pageIndex,@RequestParam(value="rows",defaultValue="10") Integer pageSize,MerchandiseUnitInfo unit_info,String sort,String order){
		DataGridResult<MerchandiseUnitInfo> result=new DataGridResult<MerchandiseUnitInfo>();
		
		MerchandiseUnitInfoCriteria ex=new MerchandiseUnitInfoCriteria();
		MerchandiseUnitInfoCriteria.Criteria cr1= ex.createCriteria();
		
		if (null!=unit_info){
			if ((unit_info.getUnitName()!=null) && (unit_info.getUnitName().trim().length()>0)){
				cr1.andUnitNameLike(unit_info.getUnitName()+"%");
			}
		}
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<MerchandiseUnitInfo> data_list=unit_info_dao.selectByExamplePage(ex);
		result.setRows(data_list);
		
		long record_count=unit_info_dao.countByExample(ex);
		result.setTotal(record_count);
		
		return result;
	}	
	

	@RequestMapping(value="/create")
	@ResponseBody
	public Result create_new(MerchandiseUnitInfo data){
		Result result=new Result();
		int i=unit_info_dao.insert(data);
		if (1==i){
			result.setMsg("保存成功！");
			result.setStatus(true);
		}else{
			result.setMsg("保存失败！");
			result.setStatus(false);
		}
		return result;
	}

	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(MerchandiseUnitInfo data){
		Result result=new Result();
		int i=unit_info_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功！");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败！");
			result.setStatus(false);
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_data(Byte id){
		Result result=new Result();
		int i=unit_info_dao.deleteByPrimaryKey(id);
		if (1==i){
			result.setMsg("删除成功！");
			result.setStatus(true);
		}else{
			result.setMsg("删除失败！");
			result.setStatus(false);
		}
		return result;
	}				
	
}
